package com.wave.wavesystem.business.controller;

import com.mybatisflex.core.paginate.Page;
import com.wave.wavesystem.business.annotation.AuthCheck;
import com.wave.wavesystem.business.pojo.dto.user.UserQueryRequest;
import com.wave.wavesystem.business.pojo.emums.user.UserGenderEnum;
import com.wave.wavesystem.business.pojo.emums.user.UserRoleEnum;
import com.wave.wavesystem.business.pojo.entity.UserEntity;
import com.wave.wavesystem.business.pojo.vo.user.UserDetailVO;
import com.wave.wavesystem.business.service.UserService;
import wave.common.pojo.entity.KV;
import wave.common.pojo.entity.Result;
import wave.common.pojo.enums.RStatusEnum;
import com.wave.wavesystem.common.util.Condition;
import com.wave.wavesystem.common.util.Func;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 夕下风波
 * @date 2025-01-21
 * @description 用户表 访问层
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户接口")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 新增
     *
     * @param user 新增实体
     */
    @Operation(summary = "新增")
    @PostMapping("/save")
    public Result<UserEntity> save(@RequestBody UserEntity user) {
        userService.save(user);
        return Result.data(userService.getById(user.getId()), RStatusEnum.SUCCESS);
    }

    /**
     * 删除/批量删除
     *
     * @param ids id集合
     */
    @Operation(summary = "删除")
    @PostMapping("/remove")
    public Result<Boolean> remove(String ids) {
        List<String> idList = Func.strToStringList(ids);
        return Result.status(userService.removeByIds(idList), RStatusEnum.SUCCESS);
    }

    /**
     * 更新
     *
     * @param user 更新内容
     */
    @Operation(summary = "修改")
    @PostMapping("/update")
    public Result<UserEntity> update(@RequestBody UserEntity user) {
        userService.updateById(user);
        return Result.data(userService.getById(user.getId()), RStatusEnum.SUCCESS);
    }

    /**
     * 详情查询
     *
     * @param user 查询条件
     */
    @Operation(summary = "详情")
    @GetMapping("/detail")
    public Result<UserDetailVO> detail(UserEntity user) {
        UserEntity userEntity = userService.getById(user.getId());
        if (Func.isEmpty(userEntity)) return Result.data(null);
        UserDetailVO userDetailVO = new UserDetailVO();
        Func.beanCopy(userEntity, userDetailVO);
        return Result.data(userDetailVO);
    }

    /**
     * 管理员详情查询
     *
     * @param user 查询条件
     */
    @Operation(summary = "详情")
    @GetMapping("/admin/detail")
    public Result<UserEntity> adminGetUserDetail(UserEntity user) {
        return Result.data(userService.getById(user.getId()));
    }

    /**
     * 列表查询
     *
     * @param userQueryRequest 查询条件
     */
    @Operation(summary = "列表")
    @GetMapping("/list")
    public Result<List<UserEntity>> list(UserQueryRequest userQueryRequest) {
        UserEntity userEntity = new UserEntity();
        Func.beanCopy(userQueryRequest, userEntity);
        return Result.data(userService.list(Condition.getQueryWrapper(userEntity)));
    }

    /**
     * 分页查询
     *
     * @param userQueryRequest 查询条件
     */
    @Operation(summary = "分页")
    @GetMapping("/admin/page")
    @AuthCheck(mastRole = UserRoleEnum.ADMIN)
    public Result<Page<UserEntity>> page(UserQueryRequest userQueryRequest) {
        UserEntity userEntity = new UserEntity();
        Func.beanCopy(userQueryRequest, userEntity);
        return Result.data(userService.page(new Page<>(userQueryRequest.getPageNum(), userQueryRequest.getPageSize()),
                Condition.getQueryWrapper(userEntity)));
    }

    /**
     * 获取性别枚举
     */
    @Operation(summary = "性别列表")
    @GetMapping("/genderList")
    public Result<List<KV<String, Integer>>> genderList() {
        return Result.data(UserGenderEnum.list());
    }

}